﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TokenAuthority
{
    public static class Utils
    {
        public static string Hash(string input)
        {
            Random rnd = new Random();
            Byte[] salt = new Byte[8];
            rnd.NextBytes(salt);
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] raw = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] merged = merge(raw, salt);
            byte[] hash = md5.ComputeHash(merged);
            return Convert.ToBase64String(merge(merged, salt));
        }

        private static byte[] merge(byte[] a, byte[] b)
        {
            byte[] dest = new byte[a.Length + b.Length];
            Array.Copy(a, dest, a.Length);
            Array.Copy(b, 0, dest, a.Length, b.Length);
            return dest;
        }
    }
}
